package com.komorebishao.crm.controller;

import com.github.pagehelper.PageInfo;
import com.komorebishao.crm.dto.CustomerQuery;
import com.komorebishao.crm.pojo.CrmCustomer;
import com.komorebishao.crm.pojo.CrmDict;
import com.komorebishao.crm.service.CustomerService;
import com.komorebishao.crm.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: KomorebiShao
 * @Description:
 * @Date: Created in 2020-07-03
 * @Modified By:
 */
@Controller
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private DictService dictService;
    @Value("${fromType}")
    private String fromType;
    @Value("${industryType}")
    private String industryType;
    @Value("${levelType}")
    private String levelType;


//    查询客户列表
    @RequestMapping("/customerList")
    public String showCustomerList(Model model, CustomerQuery customerQuery){
        //如果pojo参数名称是类名小写开头，mvc会自动将这个pojo放入request作用域中
        //页面上只需要从request作用域中找到这个pojo，就可以访问里面的参数

        //查询的参数带回页面显示
//        model.addAttribute("custName",customerQuery.getCustName());
//        model.addAttribute("custSource",customerQuery.getCustSource());
//        model.addAttribute("custIndustry",customerQuery.getCustIndustry());
//        model.addAttribute("custLevel",customerQuery.getCustLevel());


        PageInfo<CrmCustomer> pageInfo = customerService.selectCustomerList(customerQuery);
        //客户列表数据
        List<CrmCustomer> customers = pageInfo.getList();
        //将总页数传给页面
        model.addAttribute("pages",pageInfo.getPages());
        model.addAttribute("customers",customers);
        //客户来源
        List<CrmDict> fromTypes = dictService.selectDictByTypeCode(fromType);
        model.addAttribute("fromType",fromTypes);
        //客户行业
        List<CrmDict> industryTypes = dictService.selectDictByTypeCode(industryType);
        model.addAttribute("industryType",industryTypes);
        //客户级别
        List<CrmDict> levelTypes = dictService.selectDictByTypeCode(levelType);
        model.addAttribute("levelType",levelTypes);
        return "customer";
    }


    @ResponseBody
    //根据id获取客户信息
    @RequestMapping("/customer/edit")
    public CrmCustomer getCustomerById(Integer id){
        return customerService.selectById(id);
    }

    @ResponseBody
    @RequestMapping("customer/update")
    public String updateCustomerById(CrmCustomer crmCustomer){
        boolean update = customerService.updateCustomer(crmCustomer);
        if (update){
            return "\"msg\":\"update success\"";
        }
        return "\"msg\":\"update failed\"";
    }

    @ResponseBody
    @RequestMapping("customer/delete")
    public String deleteCustomerById(Long id){
        if(customerService.deleteCustomerById(id)){
            return "{\"msg\":\"delete success\"}";
        }
        return "{\"msg\":\"delete failed\"}";
    }
}
